<?php

namespace app\service\statistics;

class User
{
    public function __construct()
    {
        $this->user  = new \app\admin\model\user\User();
    }

    public function statistics()
    {
        //昨天新增
        $yesterday_user = $this->user
            ->where('create_time', '>=', time_stamp_start(0, 1, 0))
            ->where('create_time', '<=', time_stamp_end(0, 1, 0))
            ->count();
        //今天新增
        $today_user = $this->user
            ->where('create_time', '>', time_stamp_start(0, 0, 0))
            ->where('create_time', '<', time_stamp_end(0, 0, 0))
            ->count();
        //近七日新增
        $seven_user = $this->user
            ->where('create_time', '>', time_stamp_start(0, 7, 0))
            ->where('create_time', '<', time_stamp_end(0, 0, 0))
            ->count();
        //前七日新增
        $fourteen_user = $this->user
            ->where('create_time', '>', time_stamp_start(0, 14, 0))
            ->where('create_time', '<', time_stamp_end(0, 7, 0))
            ->count();
        return [
            'total_user'                 => $this->user->count(),
            'today_user'                 => $today_user,
            'yesterday_rate'             => $yesterday_user ? bcdiv(bcsub($today_user, $yesterday_user, 2) * 100, $yesterday_user, 2) : '0.00',
            'seven_rate'                 => $fourteen_user ? bcdiv(bcsub($seven_user, $fourteen_user, 2) * 100, $fourteen_user, 2) : '0.00',
        ];
    }
}
